import Instance from "./Instance";

export class StringUtils {

    public static get ins(): StringUtils {
        return Instance.get(StringUtils);
    }

    /**
     * 去掉前后空格
     * @param str
     * @returns {string}
     */
    public trimSpace(str: string): string {
        return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');
    }

    /**
     * 获取字符串长度，中文为2
     * @param str
     */
    public getStringLength(str: string): number {
        var strArr = str.split("");
        var length = 0;
        for (var i = 0; i < strArr.length; i++) {
            var s = strArr[i];
            if (this.isChinese(s)) {
                length += 2;
            } else {
                length += 1;
            }
        }
        return length;
    }

    /**
     * 判断一个字符串是否包含中文
     * @param str
     * @returns {boolean}
     */
    public isChinese(str: string): boolean {
        var reg = /^.*[\u4E00-\u9FA5]+.*$/;
        return reg.test(str);
    }

    /**
     * 格式化字符串 "{0},{1}.format("text0","text1")
     */
    public format(val: string, ...param: any[]): string {
        for (let i = 0, len = param.length; i < len; i++) {
            let reg = new RegExp("({)" + i + "(})", "g");
            val = val.replace(reg, param[i]);
        }
        return val;
    }
    /**
     * 根据指定内容和参数生成格式化字符串
     *
     * @param Content 原始字符串内容
     * @param args 可变参数列表，用于替换原始字符串中的占位符
     * @returns 格式化后的字符串
     */
    public getLang(Content: string, ...args: any[]): string {
        var content: string = Content;
        for (var i: number = 0; i < args.length; i++) {
            content = content.replace(new RegExp(`\\{${i}\\}`, "g"), String(args[i]));
        }
        return content;
    }
    /**阿拉伯数字转汉字 */
    public static convertNumToChinese(_num: number): string {
        if (_num === 0) {
            return '零';
        }
        const hanziNum = { 0: '零', 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七', 8: '八', 9: '九' };
        const units = ['个', '万', '亿', '万亿', '兆'];
        const suffix = ['', '十', '百', '千'];
        let numString = _num + '';
        if (!(/^\d+$/.test(numString))) {
            throw new Error('Not a number');
        }
        if (numString.length > 20) {
            throw new Error('Number is too large');
        }
        let digitList = numString.split('');
        digitList.reverse();
        let splitNumList = [];
        let l = digitList.splice(0, 4);
        while (l.length) {
            splitNumList.push(l);
            l = digitList.splice(0, 4);
        }
        let hanzi = '';
        splitNumList.forEach((arr, i) => {
            let rst = '';
            arr.forEach((digit, j) => {
                rst = hanziNum[digit] + suffix[j] + rst;
            });
            rst += units[i % 6];
            hanzi = rst + hanzi;
        });
        suffix.forEach(item => (hanzi = hanzi.replace(new RegExp('零' + item, 'g'), '零')));
        for (let i = units.length - 1; i >= 0; --i) {
            let val = units[i];
            hanzi = hanzi.replace(new RegExp('(零+)' + val, 'g'), (match, $1) => ($1.length === 4 ? '' : val));
        }
        hanzi = hanzi.replace(/零+/g, '零');
        hanzi = hanzi.replace(/个/g, '');
        hanzi = hanzi.replace(/^一十/, '十');
        return hanzi;
    }

}